﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace EDMS.SMS
{
    public partial class MessageList : System.Web.UI.Page
    {
        DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && !IsCallback)
            {
                Frame.Global.GlobalFunctions.SetGridView(gridView, false);
                Session["sqlstr"] = null;
                Session["sqlParams"] = null;
            }
            DDataBind();
        }

        DataTable table;
        public DataTable Table
        {
            get
            {
                if (Session["sqlstr"] != null && Session["sqlParams"] != null)
                {
                    table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(Session["sqlstr"].ToString(), Session["sqlParams"] as SqlParameter[]);
                }
                return table;
            }
        }

        void DDataBind()
        {
            if (Session["sqlstr"] != null && Session["sqlParams"] != null)
            {
                gridView.DataSource = Table;
                gridView.DataBind();
            }
        }

        protected void gridView_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            string SqlstrFormat = @"SELECT	* FROM	dbo.Dxjl dx WHERE {0} 1=1";

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            List<SqlParameter> Params = new List<SqlParameter>();

            if (e.Parameters.Equals("-2"))
            {
                gridView.Settings.ShowPreview = true;
            }
            else if (e.Parameters.Equals("-3"))
            {
                gridView.Settings.ShowPreview = false;
            }
            else if (e.Parameters.Equals("-4"))
            {
                #region 条件查询

                if (!string.IsNullOrEmpty(DateRange.Value))
                {
                    sb.Append("dx.fssj >=@BeginDate AND dx.fssj < @EndDate AND ");
                    //单个日期
                    if (DateRange.Value.Length == 10)
                    {
                        DateTime date = Convert.ToDateTime(DateRange.Value);
                        Params.Add(new SqlParameter("@BeginDate", date));
                        Params.Add(new SqlParameter("@EndDate", date.AddDays(1)));
                    }
                    else if (DateRange.Value.Length == 23) //日期段
                    {
                        string[] dates = DateRange.Value.Split('～');
                        DateTime startDate = Convert.ToDateTime(dates[0]);
                        DateTime endDate = Convert.ToDateTime(dates[1]);
                        Params.Add(new SqlParameter("@BeginDate", startDate));
                        Params.Add(new SqlParameter("@EndDate", endDate.AddDays(1)));
                    }
                }
                if (!string.IsNullOrEmpty(TelePhone.Value))
                {
                    sb.Append("(dx.mbsjhm LIKE @Phone + '%' OR dx.bjhm LIKE @Phone +'%') AND ");
                    Params.Add(new SqlParameter("@Phone", TelePhone.Value));
                }

                Session["sqlstr"] = string.Format(SqlstrFormat, sb.ToString());
                Session["sqlParams"] = Params.ToArray();
                gridView.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(string.Format(SqlstrFormat, sb.ToString()), Params.ToArray());
                gridView.DataBind();
                #endregion
            }
            else if (e.Parameters.Equals("-1"))
            {
                //全部显示
                Session["sqlstr"] = string.Format(SqlstrFormat, sb.ToString());
                Session["sqlParams"] = Params.ToArray();
                gridView.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(string.Format(SqlstrFormat, sb.ToString()), Params.ToArray());
                gridView.DataBind();
            }
            else
            {
                #region 根据类别和条件查询

                sb.Append("dx.flag =@Flag AND ");
                Params.Add(new SqlParameter("@Flag", e.Parameters));

                if (!string.IsNullOrEmpty(DateRange.Value))
                {
                    sb.Append("dx.fssj >=@BeginDate AND dx.fssj < @EndDate AND ");
                    //单个日期
                    if (DateRange.Value.Length == 10)
                    {
                        DateTime date = Convert.ToDateTime(DateRange.Value);
                        Params.Add(new SqlParameter("@BeginDate", date));
                        Params.Add(new SqlParameter("@EndDate", date.AddDays(1)));
                    }
                    else if (DateRange.Value.Length == 23) //日期段
                    {
                        string[] dates = DateRange.Value.Split('～');
                        DateTime startDate = Convert.ToDateTime(dates[0]);
                        DateTime endDate = Convert.ToDateTime(dates[1]);
                        Params.Add(new SqlParameter("@BeginDate", startDate));
                        Params.Add(new SqlParameter("@EndDate", endDate.AddDays(1)));
                    }
                }
                if (!string.IsNullOrEmpty(TelePhone.Value))
                {
                    sb.Append("(dx.mbsjhm LIKE @Phone + '%' OR dx.bjhm LIKE @Phone +'%') AND ");
                    Params.Add(new SqlParameter("@Phone", TelePhone.Value));
                }
                Session["sqlstr"] = string.Format(SqlstrFormat, sb.ToString());
                Session["sqlParams"] = Params.ToArray();
                gridView.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(string.Format(SqlstrFormat, sb.ToString()), Params.ToArray());
                gridView.DataBind();
                #endregion
            }
        }
    }
}